package online.misscl.lsy.controller.back;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import online.misscl.lsy.controller.BaseController;
import online.misscl.lsy.entity.VaccineUser;
import online.misscl.lsy.entity.dto.LoginDTO;
import online.misscl.lsy.entity.dto.VaccineUserUpdateDTO;
import online.misscl.lsy.entity.dto.vo.TableDataInfoVo;
import online.misscl.lsy.service.VaccineUserService;
import online.misscl.lsy.util.AjaxResult;
import online.misscl.lsy.util.JwtUtils;
import online.misscl.lsy.util.Dto2Entity;
import online.misscl.lsy.util.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * project name: IntelliJ IDEA
 * Date: 2023/3/11 13:01
 *
 * @PackageName: online.misscl.lsy.controller.back
 * @ClassName: UserController
 * @Author: code mover
 * @Version:
 * @Description:
 */
@RestController
@RequestMapping("/back")
public class UserController extends BaseController{

    @Autowired
    private VaccineUserService vaccineUserService;


    @ApiOperation(value = "后台登录",notes = "")
    @PostMapping("/login")
    public AjaxResult userLogin(@RequestBody @Validated LoginDTO loginDTO) {
        System.out.println(loginDTO.toString());
        LambdaQueryWrapper<VaccineUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VaccineUser::getUsername, loginDTO.getUsername());
        VaccineUser user = vaccineUserService.getOne(queryWrapper);
        // 验证账号
        if(user == null) {
            return AjaxResult.error("账号不存在！！");
        }
        // 验证密码
        if(!user.getPassword().equals(DigestUtils.md5DigestAsHex(loginDTO.getPassword().getBytes()))) {
            return AjaxResult.error("密码有误！！");
        }
        return AjaxResult.success("登录成功", JwtUtils.generateToken(user.getUserId(),user.getAuth()));
    }
    @ApiOperation(value = "分页查询用户",notes = "")
    @GetMapping("/users/{pageSize}/{pageNum}")
    public TableDataInfoVo<List<VaccineUser>> list(@PathVariable Integer pageSize, @PathVariable Integer pageNum){
        List<VaccineUser> list = vaccineUserService.list();
        Page<VaccineUser> page = PageUtil.getPages(pageSize, pageNum, list);
        return getDataTable(page.getRecords(),page.getTotal());
    }

    @ApiOperation(value = "查询单个用户",notes = "")
    @GetMapping("/user/{userId}")
    public AjaxResult getOne(@PathVariable Integer userId){
        LambdaQueryWrapper<VaccineUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VaccineUser::getUserId,userId);
        VaccineUser one = vaccineUserService.getOne(wrapper);
        return AjaxResult.success(one);
    }

    @ApiOperation(value = "根据id删除用户",notes = "")
    @DeleteMapping("/user/{userId}")
    public AjaxResult deleteUser(@PathVariable Integer userId){
        return toAjax(vaccineUserService.removeById(userId));
    }

    @ApiOperation(value = "修改用户",notes = "")
    @PutMapping("/users")
    public AjaxResult updateUser(@RequestBody VaccineUserUpdateDTO userUpdateDTO){
        VaccineUser user = new VaccineUser();
        VaccineUser populate = (VaccineUser) Dto2Entity.populate(userUpdateDTO, user);
        System.out.println(populate);
        populate.setSex("男".equals(user.getSex()) ? "0":"1");
        return toAjax(vaccineUserService.updateById(populate));
    }
    @ApiOperation(value = "添加用户",notes = "")
    @PostMapping("/users")
    public AjaxResult saveUser(@RequestBody VaccineUserUpdateDTO userUpdateDTO){

        //将dto转化成entity
        VaccineUser user = new VaccineUser();
        BeanUtils.copyProperties(userUpdateDTO,user);

        //判断用户名是否已存在
        LambdaQueryWrapper<VaccineUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VaccineUser::getUsername,user.getUsername());
        VaccineUser one = vaccineUserService.getOne(wrapper);
        if (one != null){
            //已存在改用户名
            return AjaxResult.error("该用户名已存在！");
        }

        //判断身份证是否已存在
        LambdaQueryWrapper<VaccineUser> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(VaccineUser::getIdCard,user.getIdCard());
        VaccineUser one1 = vaccineUserService.getOne(wrapper1);
        if (one1 != null){
            //已存在改用户名
            return AjaxResult.error("该身份证已存在！");
        }
        //为属性和权限赋值
        user.setSex("男".equals(user.getSex()) ? "0":"1");
        user.setAuth(0);

        return toAjax(vaccineUserService.save(user));
    }
}
